package com.lab.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lab.pojo.Device;
import com.lab.service.DeviceService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/device")
public class DeviceController {
    @Resource
    DeviceService deviceService;
    Map<String,Object> result=new HashMap<String,Object>();
    @RequestMapping(value = "/listByPage")
    public Map<String,Object> listByPage(Integer page, Integer rows){
        //设置分页参数
        page=(page-1)*rows;
        PageHelper.offsetPage(page, rows);
        //查询所有数据
        List<Device> list= deviceService.allDevices();
        //使用PageInfo封装查询结果
        PageInfo<Device> pageInfo=new PageInfo<Device>(list);
        for(Device device:list){
            if(device.getState().equals("1")){
                device.setState("正常");
            }else if(device.getState().equals("0")){
                device.setState("报废");
            }else{
                device.setState("维修");
            }
            if(device.getUsed().equals("1")){
                device.setUsed("正在使用");
            }else{
                device.setUsed("空闲");
            }
        }
        //从PageInfo对象取出查询结果
        //总记录数
        long total=pageInfo.getTotal();
        //当前页数据列表
        List<Device> deviceList=pageInfo.getList();
        result.put("total", total);
        result.put("rows", deviceList);
        return result;
    }
    @RequestMapping(value = "/scrapListByPage")
    public Map<String,Object> ScrapListByPage(Integer page, Integer rows){
        //设置分页参数
        page=(page-1)*rows;
        PageHelper.offsetPage(page, rows);
        //查询所有数据
        List<Device> list= deviceService.findDevicesScrap();
        //使用PageInfo封装查询结果
        PageInfo<Device> pageInfo=new PageInfo<Device>(list);
        for(Device device:list){
            if(device.getState().equals("1")){
                device.setState("正常");
            }else if(device.getState().equals("0")){
                device.setState("报废");
            }else{
                device.setState("维修");
            }
            if(device.getUsed().equals("1")){
                device.setUsed("正在使用");
            }else{
                device.setUsed("空闲");
            }
        }
        //从PageInfo对象取出查询结果
        //总记录数
        long total=pageInfo.getTotal();
        //当前页数据列表
        List<Device> deviceList=pageInfo.getList();
        result.put("total", total);
        result.put("rows", deviceList);
        return result;
    }
    @RequestMapping(value = "/repairListByPage")
    public Map<String,Object> RepairListByPage(Integer page, Integer rows){
        //设置分页参数
        page=(page-1)*rows;
        PageHelper.offsetPage(page, rows);
        //查询所有数据
        List<Device> list= deviceService.findDevicesRepair();
        //使用PageInfo封装查询结果
        PageInfo<Device> pageInfo=new PageInfo<Device>(list);
        for(Device device:list){
            if(device.getState().equals("1")){
                device.setState("正常");
            }else if(device.getState().equals("0")){
                device.setState("报废");
            }else{
                device.setState("维修");
            }
            if(device.getUsed().equals("1")){
                device.setUsed("正在使用");
            }else{
                device.setUsed("空闲");
            }
        }
        //从PageInfo对象取出查询结果
        //总记录数
        long total=pageInfo.getTotal();
        //当前页数据列表
        List<Device> deviceList=pageInfo.getList();
        result.put("total", total);
        result.put("rows", deviceList);
        return result;
    }
    @RequestMapping(value = "/usedListByPage")
    public Map<String,Object> UsedListByPage(Integer page, Integer rows){
        //设置分页参数
        page=(page-1)*rows;
        PageHelper.offsetPage(page, rows);
        //查询所有数据
        List<Device> list= deviceService.findDevicesUsed();
        //使用PageInfo封装查询结果
        PageInfo<Device> pageInfo=new PageInfo<Device>(list);
        for(Device device:list){
            if(device.getState().equals("1")){
                device.setState("正常");
            }else if(device.getState().equals("0")){
                device.setState("报废");
            }else{
                device.setState("维修");
            }
            if(device.getUsed().equals("1")){
                device.setUsed("正在使用");
            }else{
                device.setUsed("空闲");
            }
        }
        //从PageInfo对象取出查询结果
        //总记录数
        long total=pageInfo.getTotal();
        //当前页数据列表
        List<Device> deviceList=pageInfo.getList();
        result.put("total", total);
        result.put("rows", deviceList);
        return result;
    }
    /**
     * 添加和修改
     * @param device
     * @return
     */
    @RequestMapping(value="/saveDevice",method = RequestMethod.POST)
    public Map<String,Object> saveDevice(Device device){
        try {
            deviceService.saveDevice(device);
            result.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    /**
     * 数据回显
     * @param did
     * @return
     */
    @RequestMapping(value="/findDeviceById",method = RequestMethod.GET)
    public Device findDeviceById(Long did) {
        Device device= deviceService.findDeviceById(did);
        return device;
    }

    /**
     *删除数据
     * @param did
     * @return
     */
    @RequestMapping(value="/deleteDevice",method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String,Object> deleteDevice(Long[] did){
        try {
            deviceService.deleteDevice(did);
            result.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }
}
